module ActiveShipping

	#
	# This class contains utility functions used by the rest of ActiveShipping to make
	# our lives easier.  Frankly, I don't know why they aren't in the standard library already.
	#
	class Util

		#
		# Simple method to run an http post request.  If request_body is a string, it will
		# be stuck as-is into the POST request.  If it is a hash, it will be translated into
		# a proper www-encoded string for posting.  If username is set, then the "basic"
		# authentication method is used for the connection.  If the URL is https, then it
		# will set the connection to use https.
		#
		# uri:: This is a full URL to post to
		# request_body:: This is either a string or a hash
		# username:: Optional parameter used for basic authentication
		# password:: Optional parameter used for basic authentication
		#
		def self.run_http_post_connection(uri, request_body, username = nil, password = nil)
			url = URI.parse(uri)
			http_req = Net::HTTP::Post.new(url.path)

			if username != nil
				http_req.basic_auth(username, password)
			end

			if request_body.class == String
				http_req.body = request_body
			else
				http_req.set_form_data(request_body)
			end

			conn = Net::HTTP.new(url.host, url.port)
			conn.use_ssl = url.class == URI::HTTPS ? true : false
			http_result = conn.start {|http| http.request(http_req)}

			return http_result
		end

		# Parses a string into options for a DSN.
		#
		# For example <tt>"a=b;c=d;qwerty=foo"</tt> would result in the hash <tt>{ :a => "b", :c => "d", :qwerty => "foo" }</tt>
		#
		def self.parse_dsn(str)
			options = {}
			str.split(";").each do |keyval|
				if keyval.index("=")
					key, val = keyval.split("=", 2)
					key = key.strip
					val = val.strip
					options[key.intern] = val
				end
			end

			return options
		end

		def self.us_states
			@@us_state_list
		end

		def self.us_state_map
			@@us_state_map
		end

		def self.countries
			@@country_list
		end

		def self.country_map
			@@country_map
		end

		@@us_state_list = [
			["Alabama", "AL"],
			["Alaska", "AK"],
			["American Samoa", "AS"],
			["Arizona", "AZ"],
			["Arkansas", "AR"],
			["California", "CA"],
			["Colorado", "CO"],
			["Connecticut", "CT"],
			["Delaware", "DE"],
			["District of Columbia", "DC"],
			["Federated States of Micronesia", "FM"],
			["Florida", "FL"],
			["Georgia", "GA"],
			["Guam", "GU"],
			["Hawaii", "HI"],
			["Idaho", "ID"],
			["Illinois", "IL"],
			["Indiana", "IN"],
			["Iowa", "IA"],
			["Kansas", "KS"],
			["Kentucky", "KY"],
			["Louisiana", "LA"],
			["Maine", "ME"],
			["Marshall Islands", "MH"],
			["Maryland", "MD"],
			["Massachusetts", "MA"],
			["Michigan", "MI"],
			["Minnesota", "MN"],
			["Mississippi", "MS"],
			["Missouri", "MO"],
			["Montana", "MT"],
			["Nebraska", "NE"],
			["Nevada", "NV"],
			["New Hampshire", "NH"],
			["New Jersey", "NJ"],
			["New Mexico", "NM"],
			["New York", "NY"],
			["North Carolina", "NC"],
			["North Dakota", "ND"],
			["Northern Mariana Islands", "MP"],
			["Ohio", "OH"],
			["Oklahoma", "OK"],
			["Oregon", "OR"],
			["Palau", "PW"],
			["Pennsylvania", "PA"],
			["Puerto Rico", "PR"],
			["Rhode Island", "RI"],
			["South Carolina", "SC"],
			["South Dakota", "SD"],
			["Tennessee", "TN"],
			["Texas", "TX"],
			["Utah", "UT"],
			["Vermont", "VT"],
			["Virgin Islands", "VI"],
			["Virginia", "VA"],
			["Washington", "WA"],
			["West Virginia", "WV"],
			["Wisconsin", "WI"],
			["Wyoming", "WY"]
		]

		@@country_list = [
["United States", "US"],
["Canada", "CA"],
["Afghanistan", "AF"],
["Albania ", "AL"],
["Algeria", "DZ"],
["Andorra", "AD"],
["Angola", "AO"],
["Anguilla", "AI"],
["Antigua &amp; Barbuda", "AG"],
["Argentina", "AR"],
["Armenia", "AM"],
["Aruba", "AW"],
["Australia", "AU"],
["Austria", "AT"],
["Azerbaijan", "AZ"],
["Bahamas", "BS"],
["Bahrain", "BH"],
["Bangladesh", "BD"],
["Barbados", "BB"],
["Belarus", "BY"],
["Belgium", "BE"],
["Belize", "BZ"],
["Benin", "BJ"],
["Bermuda", "BM"],
["Bhutan", "BT"],
["Bolivia", "BO"],
["Bosnia-Herzegovina", "BA"],
["Botswana", "BW"],
["Brazil", "BR"],
["British Virgin Islands", "IO"],
["Brunei Darussalam", "BN"],
["Bulgaria", "BG"],
["Burkina Faso", "BF"],
["Burma (Myanmar)", "MM"],
["Burundi ", "BI"],
["Cambodia", "KH"],
["Cameroon", "CM"],
["Cape Verde", "CV"],
["Cayman Islands", "KY"],
["Central African Republic", "CF"],
["Chad ", "TD"],
["Chile", "CL"],
["China", "CN"],
["Colombia", "CO"],
["Comoros", "KM"],
["Congo", "CG"],
["Costa Rica", "CR"],
["Cote d'Ivoire (Ivory Coast)", "CI"],
["Croatia", "HR"],
["Cyprus", "CY"],
["Czech Republic", "CZ"],
["Denmark", "DK"],
["Djibouti", "DJ"],
["Dominica", "DM"],
["Dominican Republic", "DO"],
["Ecuador", "EC"],
["Egypt", "EG"],
["El Salvador", "SV"],
["Equatorial Guinea", "GQ"],
["Eritrea", "ER"],
["Estonia", "EE"],
["Ethiopia", "ET"],
["Faroe Islands", "FO"],
["Fiji", "FJ"],
["Finland", "FI"],
["France", "FR"],
["French Guiana", "GF"],
["French Polynesia", "PF"],
["Gabon", "GA"],
["Gambia", "GM"],
["Georgia", "GE"],
["Germany", "DE"],
["Ghana", "GH"],
["Gibraltar", "GI"],
["Great Britain", "GB"],
["Greece", "GR"],
["Greenland", "GL"],
["Grenada", "GD"],
["Guadeloupe", "GP"],
["Guatemala", "GT"],
["Guinea", "GN"],
["Guinea-Bissau", "GW"],
["Guyana", "GY"],
["Haiti", "HT"],
["Honduras", "HN"],
["Hong Kong", "HK"],
["Hungary", "HU"],
["Iceland", "IS"],
["India", "IN"],
["Indonesia", "ID"],
["Iran", "IR"],
["Ireland (Eire)", "IE"],
["Israel", "IL"],
["Italy", "IT"],
["Jamaica", "JM"],
["Japan", "JP"],
["Jordan", "JO"],
["Kazakhstan", "KZ"],
["Kenya", "KE"],
["Kiribati", "KI"],
["South Korea", "KR"],
["Kuwait", "KW"],
["Kyrgyzstan", "KG"],
["Laos", "LA"],
["Latvia", "LV"],
["Lebanon", "LB"],
["Lesotho", "LS"],
["Liberia", "LR"],
["Liechtenstein", "LI"],
["Lithuania", "LT"],
["Luxembourg", "LU"],
["Macedonia", "MK"],
["Madagascar", "MG"],
["Malawi", "MW"],
["Malaysia", "MY"],
["Maldives", "MV"],
["Mali", "ML"],
["Malta", "MT"],
["Martinique", "MQ"],
["Mauritania", "MR"],
["Mauritius", "MU"],
["Mexico", "MX"],
["Moldova", "MD"],
["Mongolia", "MN"],
["Montserrat", "MS"],
["Morocco", "MA"],
["Mozambique", "MZ"],
["Namibia", "NA"],
["Nauru", "NR"],
["Nepal", "NP"],
["Netherlands", "NL"],
["Netherlands Antilles", "AN"],
["New Caledonia", "NC"],
["New Zealand", "NZ"],
["Nicaragua", "NI"],
["Niger", "NE"],
["Nigeria", "NG"],
["Norway", "NO"],
["Oman", "OM"],
["Pakistan", "PK"],
["Panama", "PA"],
["Papua New Guinea ", "PG"],
["Paraguay", "PY"],
["Peru", "PE"],
["Philippines", "PH"],
["Poland", "PL"],
["Portugal", "PT"],
["Qatar", "QA"],
["Reunion", "RE"],
["Romania", "RO"],
["Russia", "RU"],
["Rwanda", "RW"],
["Saint Lucia", "LC"],
["Saint Vincent &amp; Grenadines", "VC"],
["San Marino ", "SM"],
["Sao Tome &amp; Principe", "ST"],
["Saudi Arabia", "SA"],
["Senegal", "SN"],
["Serbia-Montenegro (Yugoslavia)", "YU"],
["Seychelles", "SC"],
["Sierra Leone ", "SL"],
["Singapore", "SG"],
["Slovak Republic (Slovakia)", "SK"],
["Slovenia", "SI"],
["Solomon Islands", "SB"],
["Somalia ", "SO"],
["South Africa", "ZA"],
["Spain", "ES"],
["Sri Lanka", "LK"],
["St. Christopher (St. Kitts) &amp; Nevis", "KN"],
["Sudan", "SD"],
["Suriname", "SR"],
["Swaziland", "SZ"],
["Sweden", "SE"],
["Switzerland", "CH"],
["Syrian Arab Republic (Syria)", "SY"],
["Taiwan", "TW"],
["Tajikistan", "TJ"],
["Tanzania", "TZ"],
["Thailand", "TH"],
["Togo", "TG"],
["Tonga", "TO"],
["Trinidad &amp; Tobago", "TT"],
["Tunisia", "TN"],
["Turkey", "TR"],
["Turkmenistan", "TM"],
["Turks &amp; Caicos Islands", "TC"],
["Tuvalu", "TV"],
["Uganda", "UG"],
["Ukraine", "UA"],
["United Arab Emirates", "AE"],
["Uruguay", "UY"],
["Uzbekistan", "UZ"],
["Vanuatu", "VU"],
["Vatican City", "VA"],
["Venezuela", "VE"],
["Vietnam", "VN"],
["Wallis &amp; Futuna Islands", "WF"],
["Western Samoa", "EH"],
["Yemen", "YE"],
["Zambia", "ZM"],
["Zimbabwe", "ZW"]
]
		@@country_map = {}
		@@country_list.each{|x| @@country_map[x[1]] = x[0]}

		@@us_state_map = {}
		@@us_state_list.each{|x| @@us_state_map[x[1]] = x[0]}
	end
end
